<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Patient extends Model
{
    private $fields = array('patient_id','patient_name','gender','age','department_code','department','status','rfid','remark');

    function mainData($size=15,$where=''){
        $map[] = ['is_del','=','0'];
        if(input('patient') && input('patient')<>''){
            $map[] = ['p.patient_id','like','%'.input('patient').'%'];
        }
        if(input('patient_name') && input('patient_name')<>''){
            $map[] = ['p.patient_name','like','%'.input('patient_name').'%'];
        }
        if(input('rfid') && input('rfid')<>''){
            $map[] = ['p.rfid','like','%'.input('rfid').'%'];
        }
        $joinSql = "(SELECT xinpian,COUNT(id) AS record_num FROM tp_sign_record WHERE createtime>".strtotime(date("Ymd"))." GROUP BY xinpian) r";
        if(strlen(input('iswarn'))==1 ){
            if( input('iswarn') == 1 ){
                $map[] = ['p.iswarn_today','=',1];
            }else{
                $map[] = ['p.iswarn_today','=',0];
            }
        }
        if($where <> ''){
            $map = array_merge($where,$map);
        }
        // dump($map);exit;
        $count = Db::name('patient')->alias('p')->where($map)->count();// 查询满足要求的总记录数

        $list = Db::name('patient')
            ->alias('p')
            ->join($joinSql,'p.rfid = r.xinpian', 'left')
//            ->join('subject s','e.subjectid = s.subjectid','left')
           ->field('p.*,r.xinpian,r.record_num')
            ->where($map)
            ->order('id desc')
            ->paginate($size,$count,['query'=>request()->param()])
            ->each(function($item,$key){
                $item['record_num'] = $item['record_num']>0 ? $item['record_num'] : 0;
                return $item;
            });
        $page = $list->render();

        $data['count'] = $count;
        $data['list'] = $list;
        $data['page'] = $page;
        return $data;
    }

    //处理参数
    function disposeData($data,$act=''){

        $newData = array_elements($this->fields, $data);

        return $newData;
    }

}